$(function () {
    initSqlStatementApi();
});

function initSqlStatementApi() {
    var json = {};
    location.search.substring(1).replace(/([^=&]+)=([^&]*)/g, function(m, key, value) {
        json[decodeURIComponent(key)] = decodeURIComponent(value);
    });
    if(!json.id){
        return ;
    }
    $.ajax({
        url: "/dms/getById?id="+json.id,
        method: "post",
        dataType: "json",
        contentType: 'application/json',
        success: function (response) {
            for(var i = 0 ; i < response.parserResults.length ; i++){
                var data = response.parserResults[i];
                data.id = "/"+data.id.replace(/\./g,"/");
                renderMappedStatementAPI(data,i);
            }
        }
    });
}

function renderMappedStatementAPI(data,index) {
    if(!data.rest){
        return;
    }
    var html = [];
    var bookmarks = [];
    var id = "api-group"+index;
    bookmarks.push("<bookmark class='dropdown-item' name='"+data.name+"' href='"+id+"'>"+data.name+"</bookmark>");
    var formId = 'apiDetail'+index;
    html.push("<div class='api-group' id='"+id+"'>");
    html.push("<form id='"+formId+"' >");


    html.push("<div class=\"form-group row\">");
    html.push("<label  class=\"col-sm-2 col-form-label col-form-label-name\">"+data.name+"</label>");
    html.push("<div class=\"col-sm-8\">");
    html.push("</div>");
    html.push("</div>");

    html.push("<div class=\"form-group row\">");
    html.push("<label  class=\"col-sm-2 col-form-label\">"+data.method+"</label>");
    html.push("<div class=\"col-sm-8\">");
    html.push("<input type=\"text\" name='id' value='"+(data.id)+"' readonly class=\"form-control-plaintext\">");
    html.push("</div>");
    html.push("</div>");

    html.push("</form>");


    var tables = [
        {
            id : "requestHeaderTable"+index,
            title : "请求头",
            field : "requestHeader",
        },
        {
            id : "inputParameterTable"+index,
            title : "请求参数",
            field : "inputParameter",
        },
        {
            id : "outputParameterTable"+index,
            title : "响应参数",
            field : "outputParameter",
        }
    ];
    //初始化入参、出参
    for(var item in tables){
        var table = tables[item];
        html.push("<div class=\"api-box-table\">");
        html.push("<h4>"+table.title+"</h4>");
        html.push("<table id=\""+table.id+"\" class=\"api-parameter\"></table>");
        html.push("</div>");
    }
    html.push("</div>");

    $(".bookmarks").append(bookmarks.join(""));
    $(".api-box").append(html.join(""));

    //初始化数据
    $("#"+formId).deserializable(data);
    for(var i = 0 ;i < tables.length ; i++){
        initSingleParameterTable($("#"+tables[i].id)[0],data[tables[i].field],i);
    }
}


function initSingleParameterTable(element,data,i) {
    var columns;
    if(i == 0){
        columns = [
            {field : "id",title : "id",visible:false},
            { field: "field", title : "键", align : "center", width:"30%",valign : "middle"},
            { field: "defaultValue", title : "值", align : "center",width:"25%",valign : "middle"},
            { field: "required", title : "必填", align : "center",width:"10%",valign : "middle",formatter : function (value, row, index) {
               return row.validator && row.validator.required && !(row.validator.required === "false") ? "是" : "否";
            }},
            { field: "remarks", title : "备注",align : "center"},
        ];
    }else {
        columns = [
            { field: "field", title : "参数", width:"25%" },
            { field: "name", title : "名称",align : "center"},
            { field: "mergeType", title : "类型",align : "center" ,formatter: function (value, row, index) {
                    var type = row.type ;
                    if(row.type){
                        type = row.genericType ?  row.type+"("+row.genericType+")" : type;
                    }else {
                        type = "";
                    }
                    return type;
                }},
            { field: "required", title : "必填", align : "center",valign : "middle",formatter : function (value, row, index) {
                    return row.validator && row.validator.required && !(row.validator.required === "false") ? "是" : "否";
                }},
            { field: "remarks", title : "备注",align : "center"},
        ];
    }
    $(element).bootstrapTreeTable({
        data :data,
        showColumns : false,
        showRefresh : false,
        width: "100%",
        columns :  columns
    });

}